package com.wu.Backstage.check;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.wu.Backstage.entity.Po.CommodityEntityPo;
import com.wu.Backstage.entity.Po.OrderDetailsPo;
import com.wu.Backstage.entity.Po.ShoppingCartPo;
import com.wu.Backstage.entity.Po.evaluated;
import com.wu.Backstage.service.CommodityService;
import com.wu.Backstage.service.EvaluatedService;
import com.wu.Backstage.service.OrderDetailsService;
import com.wu.Backstage.tool.Tool;
import com.wu.Backstage.util.Utils;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Component
public class MiddleCommoditCheck {

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private MiddlelayerCheck middlelayerCheck;

    @Autowired
    private EvaluatedService evaluatedService;

    public static MiddleCommoditCheck middleCommoditCheck;

    @Autowired
    private OrderDetailsService orderDetailsService;

    @Autowired
    private HttpServletResponse response;

    @PostConstruct
    public void init() {
        middleCommoditCheck = this;
        middleCommoditCheck.commodityService = this.commodityService;
    }


    /**用户评论**/
    public Tool Addevaluated(evaluated eav)
    {
        evaluated eavs = evaluatedService.Addevaluated(eav);
        if(eavs!=null){
            return  middlelayerCheck.isUserAccount(eavs,"成功!");
        }else{
            return  middlelayerCheck.isUserAccount(null,"失败!");
        }

    }


    public Tool confirmReceipt(Integer id)
    {
        int flag = orderDetailsService.confirmReceipt(id);
        if(flag>0){
            return  middlelayerCheck.isUserAccount(1,"成功!");
        }else{
            return  middlelayerCheck.isUserAccount(null,"失败!");
        }
    }

    public Tool findOrderDetailsAndEmail(Integer paymentStatus, Integer orderStatus,Integer deliverGoods,String email)
    {
        List<OrderDetailsPo> order = orderDetailsService.findOrderDetailsAndEmail(paymentStatus,orderStatus,deliverGoods,email);
        if(order!=null && order.size()>0){
            return  middlelayerCheck.isUserAccount(order,"查询成功!");
        }else{
            return  middlelayerCheck.isUserAccount(order,"查询失败!");
        }
    }

    public Tool findCommodityEntityResource(String res)
    {
        List<CommodityEntityPo> com = commodityService.findCommodityEntityResource(res);
        if(com!=null && com.size()>0){
            return  middlelayerCheck.isUserAccount(com,"查询成功!");
        }else{
            return  middlelayerCheck.isUserAccount(com,"查询失败!");
        }
    }



    public Tool findOrder(Integer paymentStatus, Integer orderStatus,Integer deliverGoods){


        List<OrderDetailsPo> order = orderDetailsService.findOrder(paymentStatus,orderStatus,deliverGoods);
        if(order!=null && order.size()>0){
            return  middlelayerCheck.isUserAccount(order,"查询成功!");
        }else{
            return  middlelayerCheck.isUserAccount(order,"查询失败!");
        }
    }


    public Tool dateleOrder(Integer id){

        orderDetailsService.deleteOrderDetails(id);
        return middlelayerCheck.isUserAccount(1,"删除成功!");
    }


    public Tool findOrder(){

        List<OrderDetailsPo> order =  orderDetailsService.findOrderDetails();
        if(order!=null && order.size()>0){
            return  middlelayerCheck.isUserAccount(order,"查询成功!");
        }else{
            return  middlelayerCheck.isUserAccount(order,"查询失败!");
        }
    }


    public Tool uporderDate(Integer id){

        int isT =  orderDetailsService.UpOrderDetails(id);
        if(isT>0){
            return  middlelayerCheck.isUserAccount(1,"发货成功!");
        }else{
            return  middlelayerCheck.isUserAccount(0,"失败发货!");
        }
    }

    public Tool uporderPay(Integer id){

        int isT =  orderDetailsService.UpOrderPay(id);
        if(isT>0){
            return  middlelayerCheck.isUserAccount(1,"支付成功!");
        }else{
            return  middlelayerCheck.isUserAccount(0,"支付失败!");
        }
    }




    /**加入购物车**/
    public Tool addShoppingCard(JSONObject list)
    {
        Date date = DateUtil.date();
        ShoppingCartPo Storein = new ShoppingCartPo();
        try {
            BeanUtils.copyProperties(Storein, list);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        Storein.setJoinTime(date);
        Storein = commodityService.AddShoppingCartPo(Storein);
        if(Storein!=null)
        {
            return  middlelayerCheck.isUserAccount(1,"添加成功!");
        }
        return middlelayerCheck.isUserAccount(null,"添加失败!");

    }


    /**创建订单**/
    public Tool AddUserOrder(JSONObject list)
    {
        Date date = DateUtil.date();
        OrderDetailsPo OrderDeta = new OrderDetailsPo();
        try {
            BeanUtils.copyProperties(OrderDeta, list);
            System.out.println(OrderDeta);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        OrderDeta.setOrderTime(date);
        OrderDeta.setPaymentStatus(0);
        OrderDeta.setOrdrNumber(IdUtil.fastSimpleUUID());
        OrderDeta.setOrderStatus(0);
        OrderDeta.setPaymentMethod(0);
        OrderDeta.setCompleteTime(null);
        OrderDeta.setOrderStatus(0);
        OrderDeta.setComplete(0);
        OrderDeta.setDeliverGoods(0);


        System.out.println(OrderDeta);
        OrderDetailsPo Order = orderDetailsService.AddOrderDetailsPo(OrderDeta);
        if(Order!=null)
        {
            return  middlelayerCheck.isUserAccount(1,"添加成功!");
        }
        return middlelayerCheck.isUserAccount(null,"添加失败!");

    }






    public Tool setCommodit(JSONObject jsonOb)
    {
        Utils utils = new Utils();
        CommodityEntityPo commodity = new CommodityEntityPo();
        commodity.setResource(null);
        commodity.setDesc((String) jsonOb.get("desc"));
        commodity.setName((String) jsonOb.get("name"));
        commodity.setPrice(Double.parseDouble((String) jsonOb.get("price")));
        commodity.setImgurl(jsonOb.get("imgurl").toString());
        commodity.setState(0);
        commodity.setSta(0);
        commodity.setCount(0);
        commodity.setType((String) jsonOb.get("type"));
        commodity.setRegion((String) jsonOb.get("region"));
        commodity.setDityId(IdUtil.fastSimpleUUID());
        commodity.setQuantity(Integer.parseInt((String) jsonOb.get("quantity")));
        commodity.setSalesvolumes(0);
        commodity.setUploadtime(utils.getDate());
       CommodityEntityPo com = middleCommoditCheck.commodityService.CommodityEntity(commodity);
        if(com!=null)
        {
            return  middlelayerCheck.isUserAccount(1,"添加成功!");
        }
            return middlelayerCheck.isUserAccount(null,"添加失败!");
    }








}
